#!/bin/bash
# 运行示例： 注意路径  /opt/data_warehouse/file/ads/t_kettle_run.sh /opt/data_warehouse/file/ads/ t_kettle_ads_wlh_premises_data_crm_log 2021-07-20 
# desc：根据传入的tran名称、tran路径以及跑数日期 
# author:admin
# dev_date：2021-07-22
# note :注意变量 与脚本中的路径并相应调整
source ~/.bash_profile
cd "$KETTLE_HOME"
rep_name=shzt_temp
tran_name=${2}
tran_dir=${1}
param_date=${3//-/}
param_date2=${3}
data_level=ads

#判断Log_dir 是否存在,不存在则创建目录
function checkmkdir()
{
if [ ! -d "${log_dir}" ]; then
  mkdir -p ${log_dir}
 if [ $? -eq 0 ];then
  echo "create dir ${log_dir} success" 
else 
 echo "create dir ${log_dir}  fail"
exit 1 
  fi
 else 
echo "log dir is ${log_dir}"
fi
}
#判断日期格式是否正确
function checkdate()
{
year=${param_date:0:4}
month=${param_date:4:2}
day=${param_date:6:2}
if [ ${#param_date} -ne 8 ];     
then 
	echo "Usage: bash $0 yyyymmdd format"
    exit 2
fi
if echo $day|grep -q '^0'
then
    day=`echo $day |sed 's/^0//'`
fi
if cal $month $year >/dev/null 2>/dev/null
then
    daym=`cal $month $year|egrep -v "$year|Su"|grep -w "$day"`
    if [ "$daym" != "" ]
    then
	log_dir=/opt/data_warehouse/log/${param_date}/${data_level}
    log_name=${tran_name}_${param_date}.log
        echo ok
		checkmkdir
    else
        echo "Error: Please input a right date."
    exit 2
    fi
else
    echo "Error: Please input a right date."
    exit 3
fi	
}
# 执行kettle 转换,根据传入的tran名称、tran路径以及跑数日期
function execjob()
{ 
echo “running the ${tran_name}”
./pan.sh  -file=${tran_dir}${tran_name}.ktr -level:Detailed -param:DATA_DATE1=${param_date} -param:DATA_DATE2=${param_date2}  >>${log_dir}/${log_name} 2>&1
if [ $? -eq 0 ];then
 echo "the job run sucess....see the log info ${log_dir}/${tran_name}_${param_date}.log"
 else 
  echo " the job run fail,check the log info   ${log_dir}/${tran_name}_${param_date}.log" 
exit 3
fi
}
#主函数
function main()
{
checkdate ${tran_name} ${tran_dir} ${param_date}
execjob ${tran_name} ${tran_dir} ${param_date}
}
main ${tran_name} ${tran_dir} ${param_date}
